package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.MemberOrder;
import com.itheima.pojo.Role;
import com.itheima.service.MemberService;
import com.itheima.service.RoleService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private MemberService memberService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return memberService.pageQuery(queryPageBean);
    }

    /**
     * 添加预约信息
     * @param memberOrder 表单成员信息
     * @param setmealIds  表单套餐id
     * @return
     */
    @RequestMapping("/insert")
    public Result insert(@RequestBody MemberOrder memberOrder, Integer[] setmealIds){
        try{
            memberService.insert(memberOrder,setmealIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"预约信息添加失败");//新增失败
        }
        return new Result(true,"预约信息添加成功");//新增成功
    }

    /**
     * 用户发送确认预约消息
     * @param telephone
     * @return
     */
    @RequestMapping("/send4Order")
    public Result send4Order(String telephone){
        //随机生成6位数字验证码
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        try{
            //调用工具类发送短信
           SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,validateCode.toString());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"确认预约信息发送失败");
        }
        return new Result(true,"确认预约信息发送成功");
    }

    /**
     * 根据id删除成员信息
     * @param id
     * @return
     */
    @RequestMapping("/orderDelete")
    public Result orderDelete(Integer id){
        try{
            memberService.orderMake(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
        return new Result(true,"删除成功");
    }
    /**
     * 用户发送确认预约消息
     * @param telephone
     * @return
     */
    @RequestMapping("/handleOrder")
    public Result handleOrder(String telephone){
        //随机生成6位数字验证码
        Integer validateCode = ValidateCodeUtils.generateValidateCode(6);
        try{
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,telephone,validateCode.toString());
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"取消预约信息发送失败");
        }
        return new Result(true,"取消预约信息发送成功");
    }

    /**
     * 批量删除
     * @param delIds
     * @return
     */
    @RequestMapping("/delOrderById")
    public Result delOrderById(Integer[] delIds){
        //调用service层中的方法，删除选中的数据
        try {
            memberService.delOrderById(delIds);
            //删除成功
            return new Result(true,"批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false,"批量删除失败");
        }
    }

}
